/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import de.matthiasmann.twl.utils.PNGDecoder;
import de.matthiasmann.twl.utils.PNGDecoder.Format;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;

/**
 *
 * @author ctuchsch
 */
public class Util
{

    public static void attachShaders(int ProgramHandle, int... ShaderHandles)
    {
        for (int ShaderHandle : ShaderHandles)
            glAttachShader(ProgramHandle, ShaderHandle);
    }

    public static int loadShader(String filename, int type) throws IOException
    {
        StringBuilder shaderSource = new StringBuilder();
        int shaderID;

        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = reader.readLine()) != null)
                shaderSource.append(line)
                        .append("\n");

        } catch (IOException e) {
            String msg = "Could not load shader " + filename + " of type " + String
                    .valueOf(type);
            System.err.println(msg);
            throw e;
        }

        shaderID = glCreateShader(type);
        glShaderSource(shaderID, shaderSource);
        glCompileShader(shaderID);

        return shaderID;
    }

    public static float coTangent(float angle)
    {
        return (float) (1f / Math.tan(angle));
    }

    public static float degreesToRadians(float degrees)
    {
        return degrees * (float) (Math.PI / 180d);
    }

    public static float[] ConcatArrayf(float[]... b)
    {
        
        if (b.length < 1)
            return null;
        else if (b.length < 2)
            return b[0];

        int totalLength = 0;
        for (float[] fArray : b)
            totalLength += fArray.length;

        float[] retVal = new float[totalLength];

        int offset = 0;
        for (float[] fArray : b) {
            System.arraycopy(fArray, 0, retVal, offset, fArray.length);
            offset += fArray.length;
        }
        return retVal;
    }

    public static int loadTexture(String filename) throws IOException
    {
        ByteBuffer buf;
        int tWidth = 0;
        int tHeight = 0;
        int textureHandle;

        try (InputStream in = new FileInputStream(filename)) {
            PNGDecoder decoder = new PNGDecoder(in);

            // Get the width and height of the texture
            tWidth = decoder.getWidth();
            tHeight = decoder.getHeight();


            // Decode the PNG file in a ByteBuffer
            buf = ByteBuffer.allocateDirect(
                    4 * decoder.getWidth() * decoder.getHeight());
            decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
            buf.flip();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        textureHandle = glGenTextures();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, textureHandle);
        
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        
        glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, tWidth, tHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
        
        glGenerateMipmap(GL_TEXTURE_2D);
        
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        
        return textureHandle;
    }            
}
